package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionBackwashingMapper;
import com.guofen.pojo.SjqWaterProductionBackwashing;
import com.guofen.service.sjq.SjqWaterProductionBackwashingService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqWaterProductionBackwashingServiceImpl implements SjqWaterProductionBackwashingService {
    @Autowired
    private SjqWaterProductionBackwashingMapper sjqWaterProductionBackwashingMapper;

    @Override
    public Boolean addontime1(int num) {
        if(sjqWaterProductionBackwashingMapper.count()==0) {
            SjqWaterProductionBackwashing sjqWaterProductionBackwashing = new SjqWaterProductionBackwashing();
            String date = DateAndTimeUtil.getDate();
            System.out.println("当前日期" + date);
            sjqWaterProductionBackwashing.setDate(date);
            sjqWaterProductionBackwashing.setActualBackwashingDate(date);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String time = df.format(new Date());
            System.out.println("当前时间：" + time);
            if (num == 0) {
                sjqWaterProductionBackwashing.setBackwashingPumpOpenTime1(time);
            }
            int result = sjqWaterProductionBackwashingMapper.insert(sjqWaterProductionBackwashing);
            if (result == 1)
                return true;
            else
                return false;
        }
        else{
            SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
            String date = DateAndTimeUtil.getDate();
            System.out.println("当前日期" + date);
            sjqWaterProductionBackwashing.setDate(date);
            sjqWaterProductionBackwashing.setActualBackwashingDate(date);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String time = df.format(new Date());
            System.out.println("当前时间：" + time);
            if (num == 0) {
                sjqWaterProductionBackwashing.setBackwashingPumpOpenTime1(time);
            }
            int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
            if (result == 1)
                return true;
            else
                return false;
        }
    }

    @Override
    public Boolean addofftime1(int num) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println("当前时间："+time);
        if(num==1){
            sjqWaterProductionBackwashing.setBackwashingPumpCloseTimer1(time);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addisfive1(int num, String yn, String reason) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        if(num==2){
            sjqWaterProductionBackwashing.setIsFlowmeter51(yn+' '+reason);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addontime2(int num) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println("当前时间："+time);
        if(num==3){
            sjqWaterProductionBackwashing.setBackwashingPumpOpenTime2(time);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addofftime2(int num) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println("当前时间："+time);
        if(num==4){
            sjqWaterProductionBackwashing.setBackwashingPumpCloseTimer2(time);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addisfive2(int num, String yn, String reason) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        if(num==5){
            sjqWaterProductionBackwashing.setIsFlowmeter52(yn+' '+reason);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addontime3(int num) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println("当前时间："+time);
        if(num==6){
            sjqWaterProductionBackwashing.setBackwashingPumpOpenTime3(time);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addofftime3(int num) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println("当前时间："+time);
        if(num==7){
            sjqWaterProductionBackwashing.setBackwashingPumpCloseTimer3(time);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addisfive3(int num, String yn, String reason) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        if(num==8){
            sjqWaterProductionBackwashing.setIsFlowmeter53(yn+' '+reason);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addvalue(Double value) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        sjqWaterProductionBackwashing.setMeasuredValue(value);
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addisfive4(int num, String yn, String reason) {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        if(num==9){
            sjqWaterProductionBackwashing.setIsHigher5(yn+' '+reason);
        }
        int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        if(result==1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addinfo(String drug_name, String drug_specifications, Double drug_dosage, Double add_water, String remarks) {
        if(sjqWaterProductionBackwashingMapper.count()!=0){
            SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
            sjqWaterProductionBackwashing.setDrugName(drug_name);
            sjqWaterProductionBackwashing.setDrugSpecifications(drug_specifications);
            sjqWaterProductionBackwashing.setDrugDosage(drug_dosage);
            sjqWaterProductionBackwashing.setAddWater(add_water);
            sjqWaterProductionBackwashing.setRemarks(remarks);
            int result = sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
            if(result==1)
                return true;
            else
                return false;
        }
        else{
            SjqWaterProductionBackwashing sjqWaterProductionBackwashing = new SjqWaterProductionBackwashing();
            String date = DateAndTimeUtil.getDate();
            sjqWaterProductionBackwashing.setDate(date);
            sjqWaterProductionBackwashing.setActualBackwashingDate(date);
            sjqWaterProductionBackwashing.setDrugName(drug_name);
            sjqWaterProductionBackwashing.setDrugSpecifications(drug_specifications);
            sjqWaterProductionBackwashing.setDrugDosage(drug_dosage);
            sjqWaterProductionBackwashing.setAddWater(add_water);
            sjqWaterProductionBackwashing.setRemarks(remarks);
            int result = sjqWaterProductionBackwashingMapper.insert(sjqWaterProductionBackwashing);
            if(result==1)
                return true;
            else
                return false;
        }
    }

    @Override
    public String getruntime1() {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        Double rt = sjqWaterProductionBackwashingMapper.get1();
        String rt1 = rt.toString();
        sjqWaterProductionBackwashing.setBackwashingPumpRunningTime1(rt1);
        sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        return rt1;
    }

    @Override
    public String getruntime2() {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        Double rt = sjqWaterProductionBackwashingMapper.get2();
        String rt2 = rt.toString();
        sjqWaterProductionBackwashing.setBackwashingPumpRunningTime2(rt2);
        sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        return rt2;
    }

    @Override
    public String getruntime3() {
        SjqWaterProductionBackwashing sjqWaterProductionBackwashing = sjqWaterProductionBackwashingMapper.getLast();
        Double rt = sjqWaterProductionBackwashingMapper.get3();
        String rt3 = rt.toString();
        sjqWaterProductionBackwashing.setBackwashingPumpRunningTime3(rt3);
        sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(sjqWaterProductionBackwashing);
        return rt3;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionBackwashing> list = sjqWaterProductionBackwashingMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionBackwashing> list = sjqWaterProductionBackwashingMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionBackwashingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionBackwashing record) {
        return sjqWaterProductionBackwashingMapper.updateByPrimaryKeySelective(record);
    }
}
